package activities.project3.group3.s12cs342;

import java.util.ArrayList;

import tools.Project3.Group3.s12cs342.AsyncAuth;
import tools.Project3.Group3.s12cs342.AsyncGetScore;
import DataStructures.Project3.Group3.s12cs342.QuestionContainer;
import DataStructures.Project3.Group3.s12cs342.QuizContainer;
import DataStructures.Project3.Group3.s12cs342.XMLParser;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import tools.Project3.Group3.s12cs342.AsyncDownload;

public class QuizMasterActivity extends Activity {
	
	ArrayList<String> s = new ArrayList<String>(); //List of quiz names
	ArrayList<QuizContainer> qc;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Quiz mApplication = ((Quiz)getApplicationContext());
        mApplication.resetScores();
        
        SharedPreferences app_preferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		String pass = app_preferences.getString("pass", "No Item");
        
        new AsyncGetScore("group3", ((Quiz)getApplicationContext()),pass).execute();
       
       Button grades = (Button) findViewById(R.id.bGrades);
       Button logout = (Button) findViewById(R.id.bLogout);
      
		 final ListView lv = (ListView) findViewById(R.id.quizlist);	//Find the id's from main layout
		 XMLParser firstQuiz = new XMLParser(getApplicationContext());
	       
	       
	       String t = firstQuiz.parseXML();		//Parse XML File
	       qc = firstQuiz.getQuizzes();
	      
	  
	       for(QuizContainer q: qc){
	       s.add("Quiz: " + q.getQuizName() + "            Description: " + q.getQuizDescription() + "           Subject: " + q.getQuizSubject()); //Add to listview
	       }
	       
	       lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,s));   //Create the list view
	       
	       
	       
	       lv.setClickable(true);
	       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

	         public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
	        	 Intent i = new Intent(QuizMasterActivity.this, TakeQuizActivity.class);
	 			ArrayList<QuestionContainer> arraylist = qc.get(position).getQuizQuestions();  
	 			Quiz mApplication = ((Quiz)getApplicationContext());
	 			mApplication.setQuestions(arraylist);
	 			mApplication.setTestName(qc.get(position).getQuizName());
	 			mApplication.setTestDescription(qc.get(position).getQuizDescription());
	 			mApplication.setId(position);
	 			startActivity(i);
	           
	         }
	       });
       
       
       grades.setOnClickListener(new OnClickListener(){   
    	   
		public void onClick(View v) {
			
			Intent i = new Intent(QuizMasterActivity.this, GradesActivity.class);
			startActivity(i);
		}
       });
       
       
       logout.setOnClickListener( new OnClickListener(){

		public void onClick(View v) {
			Intent i = new Intent(QuizMasterActivity.this, AuthenticateActivity.class);
			startActivity(i);
		}
    	   
       });
       
       
    }
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		SharedPreferences app_preferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		String pass = app_preferences.getString("pass", "No Item");
        
        new AsyncGetScore("group3", ((Quiz)getApplicationContext()), pass).execute();
		
	}
    
   
  
}
